package com.niit.bc.servlet;

import com.niit.bc.bean.ItemBean;
import com.niit.bc.bean.OrderBean;
import com.niit.bc.biz.ItemBiz;
import com.niit.bc.biz.OrderBiz;
import com.niit.bc.biz.impl.ItemBizImpl;
import com.niit.bc.biz.impl.OrderBizImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Preview extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");

        String ContactMan = request.getParameter("ContactMan");
        String Address = request.getParameter("Address");
        String ZipCode = request.getParameter("ZipCode");
        String Email = request.getParameter("Email");
        String Mobile = request.getParameter("Mobile");
        String QQ = request.getParameter("QQ");
        String PaymentType = request.getParameter("PaymentType");
        String DeliverType = request.getParameter("DeliverType");
        String Remark = request.getParameter("Remark");
        String OrderId = request.getParameter("orderId");

		/*封装*/
        OrderBean orderBean = new OrderBean();
        orderBean.setOrderName(ContactMan);
        orderBean.setOrderAdress(Address);
        orderBean.setOrderPost(ZipCode);
        orderBean.setOrderEmail(Email);
        orderBean.setOrderMobile(Mobile);
        orderBean.setOrderQq(QQ);
        orderBean.setOrderMoneyWay(PaymentType);
        orderBean.setOrderDeliveryWay(DeliverType);
        orderBean.setOrderDetail(Remark);

        //根据订单号查询订单

        List <ItemBean> item = new ArrayList <ItemBean>();
        ItemBiz biz = new ItemBizImpl();
        item = biz.searchItems(OrderId);
        double itemPrice = 0;
        for (int i = 0; i < item.size(); i++) {
            ItemBean itemBean = item.get(i);
            itemPrice += itemBean.getItemPrice();
        }

        OrderBiz orderBiz = new OrderBizImpl();
        int n = orderBiz.updateOrders(orderBean, OrderId);

//		System.out.println(n);
        if (n > 0) {
            request.setAttribute("orderBean", orderBean);
            request.setAttribute("item", item);
            request.setAttribute("itemPrice", itemPrice);
            request.setAttribute("OrderId", OrderId);
            request.getRequestDispatcher("preview.jsp").forward(request, response);
            return;
        }
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}
